package com.index.ylr.config;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2021/11/20 14:36
 * className: RabbitMQConfig
 * version: 1.0
 * description:
 * `@EnableRabbit`：开启RabbitMQ
 */
@EnableRabbit
@Configuration
@AutoConfigureAfter(RabbitAutoConfiguration.class)
public class RabbitMQConfig {

    /**
     * 自定义消息信息格式化方式，使用JACKSON工具进行JSON序列化传输
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 自定义消息消费重试失败策略
     * @param rabbitTemplate rabbitMQ操作模板对象
     * @return 重试失败策略处理对象
     */
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate) {
        /*
            RejectAndDontRequeueRecoverer: 重试耗尽后，直接reject，丢弃消息。默认就是这种方式
            ImmediateRequeueMessageRecoverer:重试耗尽后，返回nack，消息重新入队
            RepublishMessageRecoverer:重试耗尽后，将失败消息投递到指定的交换机
         */
        return new RejectAndDontRequeueRecoverer();
//        return new RepublishMessageRecoverer(rabbitTemplate, RabbitMQConstant.DEAD_EXCHANGE, RabbitMQConstant.DEAD_ROUTING);
    }

}
